package com.thiswhyme.sentinel.server2.controller;

import com.thiswhyme.common.expection.BusinessException;
import com.thiswhyme.common.response.ResponseCode;
import com.thiswhyme.common.response.ResponseVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.SQLSyntaxErrorException;

/**
 * controller异常处理器（取消注释开启）
 * Created by thiswhyme on 2020/8/16.
 */
//@ControllerAdvice(basePackages = "com.thiswhyme")
@Slf4j
public class ControllerExceptionHandler {

    /**
     * 处理业务异常处理
     * @param e 异常信息
     * @return
     */
    @ExceptionHandler(value = BusinessException.class)
    @ResponseBody
    public ResponseVo<String> handleBusinessException(BusinessException e) {
        log.error("业务异常, 异常信息: {}", e.getMsg(), e);
        return ResponseVo.failure(e.getCode(), e.getMsg());
    }

    /**
     * 处理sql异常
     * @param e
     * @return
     */
    @ExceptionHandler(value = SQLSyntaxErrorException.class)
    @ResponseBody
    public ResponseVo<String> handleSQLException(SQLSyntaxErrorException e) {
        log.error("sql异常, 异常信息: {}", e.getMessage(), e);
        return ResponseVo.failure(ResponseCode.SYSTEM_EXCEPTION.getCode(), "sql异常");
    }

    /**
     * 处理其他异常
     * @param e 异常信息
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ResponseVo<String> handleException(Exception e) {
        log.error("未使用BusinessException异常类或其子类实现异常定制, 异常信息: {}", e.getMessage(), e);
        return ResponseVo.failure();
    }
}
